package com.subway.babynote;
/*水*/
import com.subway.babynote.syncservice.syncapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.KeyEvent;
import android.view.View.OnClickListener;
import android.database.Cursor;

public class inputamount extends Activity {
	private static final int INVISIBLE = 0x00000004;
	static final int RG_AMOUNT = 6;
	TextView unit_view;
	EditText amount_et;
	TextView food_view;
	TextView hint_view;
	NotedbHelper notedb;
	Button login_button;
	Button change_button;
	Cursor cur;
	int col;
	int food_id;
	String category;
	Bundle bundle;
	private LinearLayout linearLayout, statistics, sync, more;
	Intent In;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inputamount);
		setTitle(R.string.note);
		In = new Intent();
		//initTabFunction();
		food_view = (TextView) findViewById(R.id.noted_view);
		unit_view = (TextView) findViewById(R.id.unit_view);
		hint_view = (TextView) findViewById(R.id.hint_view);
		amount_et = (EditText) findViewById(R.id.amount_edit);
		amount_et.setFocusable(true);
		amount_et.setInputType(InputType.TYPE_CLASS_NUMBER);
		change_button = (Button) findViewById(R.id.change_button);
		login_button = (Button) findViewById(R.id.login_button);
		bundle = this.getIntent().getExtras();
		notedb = new NotedbHelper(this);
		food_id = bundle.getInt("food_id");
		category=bundle.getString("category");
		try{
			if(syncapi.getSyncImpl().getUser().isuser){
				login_button.setVisibility(INVISIBLE);
				login_button.setClickable(false);
			}
		cur = notedb.getFood(food_id);
		
		col = cur.getColumnIndex("unit");
		unit_view.setText(cur.getString(col));
        cur.close();
		}catch(Exception e){
			 Log.v("inputamount", "get info from DB e=" + e.getMessage());
		}
		food_view.setText(bundle.getString("foodname"));
		if(!bundle.getString("foodname").matches(".*"+getResources().getStringArray(R.array.food_category)[1]+".*")){
			change_button.setVisibility(INVISIBLE);
			change_button.setClickable(false);
		}
		// unit_view.setText("ml");
		amount_et.setOnKeyListener(new EditText.OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				Log.v("babynote", "keycode=" + keyCode);
				switch (keyCode) {
				case KeyEvent.KEYCODE_ENTER:
					Log.v("BabyNote", "amount confirm ...");

					bundle.putInt("food_id", food_id);
					Float qt=Float.parseFloat(amount_et.getText().toString());
					if (qt>300){
						
					hint_view.setText(getResources().getString(R.string.invalidrange));
					return false;
					} else  {
					  hint_view.setText("");
					}
					bundle.putFloat("quantity",
							Float.parseFloat(amount_et.getText().toString()));
					
					bundle.putString("amount", amount_et.getText().toString());
					bundle.putString("unit", unit_view.getText().toString());
					bundle.putString("foodname", food_view.getText().toString());

					In.setClass(inputamount.this, inputtime.class);
					In.putExtras(bundle);
					startActivityForResult(In,RG_AMOUNT);
				
				//	startActivity(In);
					return true;
				}
				return false;
			}
		});

		change_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				bundle.putInt("food_id", food_id);
				bundle.putString("from","inputamount");
                bundle.putString("category",category);
				In.setClass(inputamount.this, choosebrand.class);
				In.putExtras(bundle);
				startActivity(In);
			}
		});
		login_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				In.setClass(inputamount.this, login.class);
				startActivity(In);
			}
		});
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		 setResult(RESULT_OK);
		 this.finish();
		Log.v("inputamoutn","on activty result");
		
	}


	public void onDestroy(){
		   notedb.close();
		   super.onDestroy();	  	   
		   
		}
}